from fastapi import APIRouter, Depends
from mysql.connector import cursor

from llm.SparkDesk import llm_spark
from llm.ZhipuAI import ZhipuAI
from llm.aliyun import OpenAI
from fastapi.responses import StreamingResponse
from sql_app.db import get_db

router = APIRouter()


@router.get("/lj/{message}")
async def read_lj(message):
    stream_response = OpenAI.get_response(message)
    return StreamingResponse(stream_response, media_type="text/event-stream")


@router.get("/spark/{message}")
async def read_spark(message):
    stream_result = llm_spark.get_response(message)
    return StreamingResponse(stream_result, media_type="text/event-stream")


@router.get("/zp/{message}")
async def read_zp(message):
    stream_result = ZhipuAI.get_response(message)
    return StreamingResponse(stream_result, media_type="text/event-stream")


@router.get("/users/")
async def get_users(db: cursor.MySQLCursor = Depends(get_db)):
    query = "SELECT * FROM sys_user"
    db.execute(query)
    result = db.fetchall()
    if result:
        return {"users": result}
    else:
        return {"error": "User not found"}
